import { getEnvBaseUrl } from '@/utils'
import { http } from '@/utils/http'
import { AppUserProfileModel } from './model/profileModel'

export const getProfileApi = function () {
  return http.get<AppUserProfileModel>('-mock/api/v1/me/profile')
}

export const updateProfileApi = function (form: AppUserProfileModel) {
  return http.post<IResData<void>>('-mock/api/v1/me/profile:update', form)
}

export const updateProfileAvatarApi = (tempFilePaths) => {
  const baseUrl = getEnvBaseUrl()
  return new Promise<boolean>((resolve, reject) => {
    uni.uploadFile({
      url: baseUrl + '-mock/api/v1/me/profile/avatar:update',
      fileType: 'image',
      filePath: tempFilePaths[0],
      name: 'file',
      success() {
        resolve(true)
      },
      fail(err) {
        uni.showToast({
          icon: 'none',
          title: '请稍后再试',
        })
        reject(err)
      },
    })
  })
}
